package cn.tedu.crm.deposit.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.tedu.crm.deposit.mapper.DepositMapper;
import cn.tedu.crm.deposit.pojo.dto.DepositQuery;
import cn.tedu.crm.deposit.pojo.dto.DepositSaveParam;
import cn.tedu.crm.deposit.pojo.entity.Deposit;
import cn.tedu.crm.deposit.pojo.vo.DepositVO;
import cn.tedu.crm.deposit.pojo.vo.DepositVOO;
import cn.tedu.crm.deposit.service.DepositService;
import cn.tedu.crm.operatelog.loggable.Loggable;
import cn.tedu.crm.operatelog.loggable.OperationModule;
import cn.tedu.crm.operatelog.loggable.OperationType;
import cn.tedu.crm.orders.mapper.OrdersMapper;
import cn.tedu.crm.orders.pojo.dto.OrdersAddParam;
import cn.tedu.crm.orders.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DepositServiceImpl implements DepositService{
    @Autowired
    private DepositMapper depositMapper;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public List<DepositVO> selectDepositList(DepositQuery depositQuery) {
        return depositMapper.select(depositQuery);
    }

    @Override
    @Loggable(module = OperationModule.DEPOSIT_MANAGEMENT,type = OperationType.INSERT)
    public void insertDeposit(DepositSaveParam depositSaveParam) {
        Deposit deposit = new Deposit();
        deposit.setFollow_up_results("20");
        deposit.setCollectionTime(new Date());
        deposit.setRecipient(StpUtil.getSession().getString("username"));
        BeanUtils.copyProperties(depositSaveParam,deposit);
        log.debug("业务层数据:{}",deposit);
        depositMapper.insert(deposit);
        OrdersAddParam ordersAddParam = new OrdersAddParam();
        String depositAmount = depositSaveParam.getDepositAmount();
        ordersAddParam.setOrderCost(new BigDecimal(depositAmount));
        ordersAddParam.setUserId(depositSaveParam.getCustomerId());
        ordersAddParam.setDesignerId(ordersMapper.selectDesignerId(depositSaveParam.getCustomerId()));
        ordersAddParam.setDepositId(deposit.getId());
        ordersService.add(ordersAddParam);
    }

    @Override
    public DepositVO selectDeposit(DepositQuery depositQuery) {
        return depositMapper.selectDeposit(depositQuery);
    }

    @Override
    public DepositVOO selectById(Long id) {
        return  depositMapper.selectByIds(id);
    }

}







